import 'package:flutter/services.dart';

class MethodChannelUtils {
  late String _channelName;

  String get channelName => _channelName;
  MethodChannel? platform;

  /// 单例模式固定格式
  MethodChannelUtils._internal();

  static MethodChannelUtils? instance;

  static getInstance() {
    instance ??= MethodChannelUtils._internal();
    return instance;
  }

  factory MethodChannelUtils() => getInstance();

  ///初始化通道
  void init(String channelName) {
    _channelName = channelName;
    platform = MethodChannel(_channelName);
  }

  ///原生调用flutter方法
  Future<Null> original2FlutterHandler(Future<dynamic> Function(MethodCall call)? handler) async {
    platform?.setMethodCallHandler(handler);
  }

  ///调用原生蓝牙广播方法 通过channel调用原生方法开启蓝牙广播(原生方法每一端都要自己写)
  Future<Null> startBleBroadcast(String ip) async {
    try {
      final int result = await platform?.invokeMethod('startBleBroadcast', ip);
    } on PlatformException catch (e) {
      print('调用原生蓝牙广播error：' + e.message.toString());
    }
  }
}
